package com.baway.main.entity

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.baway.main.R
import me.majiajie.pagerbottomtabstrip.internal.RoundMessageView
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem


/**
 * Author: Jason Zhang
 * Date: 2025/8/25
 * Desc: 自定义底部导航组件
 */
class NavTabItem @JvmOverloads constructor(private val context: Context) : BaseTabItem(context) {

    private lateinit var mIcon: ImageView
    private lateinit var mTitle: TextView
    private lateinit var mMsgView: RoundMessageView

    private lateinit var mTabTitle:String
    private lateinit var mDefaultIcon: Drawable
    private lateinit var mCheckedIcon: Drawable

    private var mChecked = false
    private var mMsgNum = 0

    private var mDefaultTextColor = 0x56000000;
    private var mCheckedTextColor = 0x56000000;


    init {
        initView()
    }


    private fun initView(){
        //加载布局
        LayoutInflater.from(context).inflate(R.layout.tab_normal_layout, this, true)
        //获取布局中的UI组件
        mIcon = findViewById(R.id.tab_icon)
        mTitle = findViewById(R.id.tab_title)
        mMsgView = findViewById(R.id.tab_msg)
    }

    /**
     * 使用@DrawableRes， @ColorRes，@StringRes注解约束参数必须为Res下的资源
     */
    fun initTabData(title: String, @DrawableRes defaultIcon: Int, @DrawableRes checkedIcon: Int, @ColorRes defaultTextColor: Int, @ColorRes checkedTextColor: Int, tabChecked: Boolean = false, msgNum: Int = 0 ){
        //标题
        mTabTitle = title
        mTitle.text = mTabTitle

        //图标
        mDefaultIcon = ContextCompat.getDrawable(context, defaultIcon)!!
        mCheckedIcon = ContextCompat.getDrawable(context, checkedIcon)!!

        mChecked = tabChecked
        mMsgNum = msgNum

        //文本颜色
        mDefaultTextColor = ContextCompat.getColor(context, defaultTextColor)
        mCheckedTextColor = ContextCompat.getColor(context, checkedTextColor)

    }


    override fun setChecked(checked: Boolean) {
        mIcon.setImageDrawable(if (checked) mCheckedIcon else mDefaultIcon)
        mTitle.setTextColor(if (checked) mCheckedTextColor else mDefaultTextColor)
        mChecked = checked
    }

    override fun setMessageNumber(number: Int) {
        mMsgView.messageNumber = number
        mMsgNum = number
    }

    override fun setHasMessage(hasMessage: Boolean) {
        mMsgView.setHasMessage(hasMessage)
    }

    override fun setTitle(title: String) {
        mTitle.text = title
        mTabTitle = title
    }

    override fun setDefaultDrawable(drawable: Drawable) {
        mDefaultIcon = drawable
        if (!mChecked){
            mIcon.setImageDrawable(mDefaultIcon)
        }
    }

    override fun setSelectedDrawable(drawable: Drawable) {
        mCheckedIcon = drawable
        if (mChecked){
            mIcon.setImageDrawable(mCheckedIcon)
        }
    }

    override fun getTitle(): String {
        return mTitle.text.toString()
    }


}